<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/9
 * Time: 16:25
 */
namespace App\Http\Controllers\Home;
use App\Common\Utils;
use App\Exceptions\InternalException;
use App\Http\Controllers\Controller;
use App\Jobs\TestJob;
use App\Models\ArticleData;
use App\Models\Articles;
use App\Models\CompanyOld;
use App\Models\DesCategory;
use App\Models\DestoonArticle;
use App\Models\DestoonCompany;
use App\Models\DestoonMember;
use App\Models\DestoonMemberMisc;
use App\Models\EscCategory;
use App\Models\Goods;
use App\Models\Mall;
use App\Models\MallData;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Exceptions\InvalidRequestException;

class IndexController extends Controller{

    /*
     * 首页
     */
    public function index(){
        //php操作redis
        //$this->redisData();
        //echo phpinfo();

        //分发任务
        TestJob::dispatch(User::find(18));
        return '企业端首页！！！';
    }


    /*
     * php操作redis
     */
    public function redisData(){
        $redis = new \Redis();  //实例化这个类
        $redis->connect('localhost', 6379);  //指定主机和端口进行连接

        //添加成员的经纬度信息
//        $redis->rawCommand('geoadd','citys','116.40','39.90','beijing');
//        $redis->rawCommand('geoadd','citys','121.47','31.23','shanghai');
//        $redis->rawCommand('geoadd','citys','114.30','30.60','wuhan');

        //获取成员的经纬度
//        $data = $redis->rawCommand('geopos','citys','beijing');

        //获取两个地理位置的距离，单位：m(米，默认)， km(千米)， mi(英里)， ft(英尺)
//        $data = $redis->rawCommand('geodist','citys','beijing','shanghai','km');
//        echo "<pre>";
//        print_r($data);
//        echo "</pre>";

        //获取成员的经纬度hash，geohash表示坐标的一种方法，便于检索和存储
//        $data = $redis->rawCommand('geohash','citys','beijing','wuhan');
//        echo "<pre>";
//        print_r($data);
//        echo "</pre>";
//        exit();

        //基于经纬度坐标的范围查询
        //查询以经纬度为114，30为圆心，100千米范围内的成员
//        $data = $redis->rawCommand('georadius','citys','114','30','100','km');

        //WITHCOORD表示获取成员经纬度
//        $data = $redis->rawCommand('georadius','citys','114','30','100','km','WITHCOORD');

        //WITHDIST表示获取到圆心的距离
//        $data = $redis->rawCommand('georadius','citys','114','30','100','km','WITHDIST');

        // ASC 根据圆心位置，从近到远的返回元素
        // DESC 根据圆心位置，从远到近的返回元素
//        $data = $redis->rawCommand('georadius','citys','114','30','1000','km','DESC');

        //基于成员位置范围查询
        //查询以武汉为圆心，100千米范围内的成员
//        $data = $redis->rawCommand('georadiusbymember', 'citys', 'wuhan', '1000', 'km');
//        echo "<pre>";
//        print_r($data);
//        echo "</pre>";
//        exit();
    }

    /*
     * 零散的东西
     */
    public function other(){
        //        throw  new  InternalException('商品未上架123');
        //       throw new InvalidRequestException('商品未上架',0);
//        Log::info('测试',array(1));
//        $orderSn = Utils::orderSn();
        //$orderSn =$this->ceshi();
//        $escData = EscCategory::get()->toArray();
//        $descData[] = [];
//        foreach ($escData as $kk=>$vv){
//            $descData[$kk]['moduleid'] = 16;
//            $descData[$kk]['catname'] = $vv['cat_name'];
//            $descData[$kk]['catdir'] = PinyinController::utf8_to($vv['cat_name']);
//            $descData[$kk]['linkurl'] = 'list.php?catid='.$vv['cat_id'];
//            $descData[$kk]['letter'] = substr($descData[$kk]['catdir'],0,1);
//            $descData[$kk]['level'] = 1;
//            $descData[$kk]['listorder'] = $vv['cat_id'];
//            $descData[$kk]['group_list'] = '3,5,6,7';
//            $descData[$kk]['group_show'] = '3,5,6,7';
//            $descData[$kk]['group_add'] = '3,5,6,7';
//            $descData[$kk]['parentid'] = $vv['parent_id'];
//            if($vv['parent_id'] > 0){
//                $descData[$kk]['arrparentid'] = '0,'.$vv['parent_id'];
//                $descData[$kk]['child'] = 0;
//                $descData[$kk]['arrchildid'] = $vv['cat_id'];
//            }else{
//                $descData[$kk]['arrparentid'] = 0;
//                $descData[$kk]['child'] = 1;
//                $descData[$kk]['arrchildid'] = $this->ceshi($vv['cat_id']);
//            }
//        }
//        foreach ($descData as $kkk=>$vvv){
//            DesCategory::insert($vvv);
//        }
//        $goodData = Goods::select('goods_id','cat_id','goods_name','goods_desc','goods_thumb')
//                  ->get()->toArray();
//        $newData = [];
//        $i=1;
//        foreach ($goodData as $kk=>$vv){
//            $newData['catid'] = $vv['cat_id'];
//            $newData['areaid'] = 1;
//            $newData['level'] = 1;
//            $newData['subtitle'] = '';
//            $newData['subtext'] = '';
//            $newData['sublink'] = '';
//            $newData['skuid'] = '';
//            $newData['step'] = '';
//            $newData['stock'] = '';
//            $newData['thumbs'] = '';
//            $newData['video'] = '';
//            $newData['relate_name'] = '';
//            $newData['relate_id'] = '';
//            $newData['relate_title'] = '';
//            $newData['n1'] = '';
//            $newData['n2'] = '';
//            $newData['n3'] = '';
//            $newData['v1'] = '';
//            $newData['v2'] = '';
//            $newData['v3'] = '';
//            $newData['prices'] = '';
//            $newData['express_name_1'] = '';
//            $newData['fee_start_1'] = 0;
//            $newData['fee_step_1'] = 0;
//            $newData['title'] = $vv['goods_name'];
//            $newData['thumb'] = $vv['goods_thumb'];
//            $newData['keyword'] = $vv['goods_name'];
//            $newData['price'] = 1000;
//            $newData['amount'] = 1000;
//            $newData['unit'] = '件';
//            $newData['username'] = 'admin';
//            $newData['groupid'] = '1';
//            $newData['company'] = '启程工业';
//            $newData['addtime'] = time();
//            $newData['adddate'] = date('Y-m-d',time());
//            $newData['ip'] = '127.0.0.1';
//            $newData['status'] = 3;
//            $newData['linkurl'] = 'show.php?itemid='.$i;
//            $id = Mall::insertGetId($newData);
//            $secondData['itemid'] = $id;
//            $secondData['content'] = $vv['goods_desc'];
//            MallData::insert($secondData);
//            unset($newData);
//            unset($secondData);
//            $i++;
//        }

//        $pwd = 'enet3601rWOYNMk';
//        $pwd = $this->dpassword('enet360','1rWOYNMk');
//        $pwd = $this->random(8);
//        echo "<pre>";
//        print_r($pwd);
//        echo "</pre>";
//        exit();

        //公司信息
//        $data = CompanyOld::get()->toArray();
//        echo "<pre>";
//        print_r($data[0]);
//        echo "</pre>";
//        $memberArr = [];
//        foreach ($data as $kk=>$val){
//            $username = PinyinController::utf8_to(mb_substr($val['company_name'],0,6),true);
//            $memberArr['username'] = $username.rand(1000,9999);
//            $memberArr['passport'] = $memberArr['username'];
//            $memberArr['company']  = $val['company_name'];
//            $random = $this->random(8);
//            $pwd = 'qcgy123456';
//            $memberArr['password']  = $this->dpassword($pwd,$random);
//            $memberArr['passsalt']  = $random;
//            $pyRandom = $this->random(8);
//            $memberArr['payword']  = $this->dpassword($pwd,$pyRandom);
//            $memberArr['paysalt']  = $pyRandom;
//            $memberArr['groupid']  = 6;
//            $memberArr['loginip']  = '127.0.0.1';
//            $memberArr['regtime']  = time();
//            $memberId = DestoonMember::insertGetId($memberArr);
//            $companyArr['userid'] = $memberId;
//            $companyArr['username'] = $memberArr['username'];
//            $companyArr['groupid'] = 6;
//            $companyArr['company'] = $val['company_name'];
//            $companyArr['type'] = '企业单位';
//            $companyArr['areaid'] = 1;
//            $companyArr['mode'] = $val['jingyingmoshi'];
//            $companyArr['capital'] = $val['zijin'];
//            $companyArr['regunit'] = '人民币';
//            $companyArr['size'] = $this->guimo($val['member']);
//            $companyArr['regyear'] = date('Y',strtotime($val['create_time']));
//            $companyArr['business'] = $val['product'];
//            $companyArr['address'] = $val['address'];
//            DestoonCompany::insert($companyArr);
//        }
//        $data = DestoonMember::select('userid','username')->get()->toArray();
//        foreach ($data as $kk=>$val){
//            DestoonMemberMisc::insert($val);
//        }
//        echo "<pre>";
//        print_r($data);
//        echo "</pre>";
//        exit();
//          $data = DestoonCompany::select('username','userid')->get()->toArray();
//          foreach ($data as $kk=>$vv){
//              $saveData['linkurl'] = 'http://shops.qcgy.com.cn/index.php?homepage='.$vv['username'];
//              DestoonCompany::where(['userid'=>$vv['userid']])->update($saveData);
//          }
//          echo "<pre>";
//          print_r($data);
//          echo "</pre>";
//          exit();


//        $data = Articles::where('content', 'like', "%日本%")->get()->toArray();
//        echo "<pre>";
//        print_r($data);
//        echo "</pre>";
//        exit();

//        $i = 1;
//        foreach ($data as $kk=>$val){
//            $article['catid'] = $val['cate_id'];
//            $article['title'] = $val['article_name'];
//            $article['introduce'] = $val['article_name'];
//            $article['keyword'] = $val['article_name'];
//            $article['username'] = 'admin';
//            $article['status'] = 3;
//            $article['addtime'] = strtotime($val['article_time']);
//            $article['ip'] = '127.0.0.1';
//            if($val['id'] > 520){
//                $article['from_type'] = '中华人民共和国工业和信息化部';
//            }else{
//                $article['from_type'] = '泵阀网';
//            }
//            $article['linkurl'] = 'show.php?itemid='.$i;
//            $articleId = DestoonArticle::insertGetId($article);
//            $articleData['itemid'] = $articleId;
//            if($articleId <= 520){
//                $articleData['content'] = '<td style="padding-left:15px; padding-right:15px; line-height:22px;"><span style="font-size:14px;">'.$val['content'].'</span></td>';
//            }else{
//                $articleData['content'] = $val['content'];
//            }
//            ArticleData::insert($articleData);
//            $i++;
//        }

//        echo "<pre>";
//        print_r($data);
//        echo "</pre>";
//        exit();


//        $keyword = "中国人民共和国www.baidu.com";
//
//        $keyword = $this->decorateSearch_pre($keyword);
//        echo "<pre>";
//        print_r($keyword);
//        echo "</pre>";
//        exit();

        //echo phpinfo();



    }


    /**
     * 模糊搜索中文分词
     */
    function decorateSearch_pre($words)
    {
        $tempArr = str_split($words);
        $wordArr = array();
        $temp = '';
        $count = 0;
        $chineseLen = 3;
        foreach($tempArr as $word){
            if ($count == $chineseLen){
                $wordArr[] = $temp;
                $temp = '';
                $count = 0;
            }

            // 中文
            if(ord($word) > 127){
                $temp .= $word;
                ++$count;
            }else if (ord($word) != 32){
                $wordArr[] = $word;
            }
        }

        if ($count == $chineseLen){
            $wordArr[] = $temp;
        }

        return '%'.implode($wordArr, '%').'%';
    }

    public function guimo($num){
        if ($num<50){
            $str = '1-49人';
        }elseif ($num>=50&& $num<100){
            $str = '50-99人';
        }elseif ($num>=100 && $num<500){
            $str = '100-499人';
        }elseif ($num>=500 && $num<1000){
            $str = '500-999人';
        }elseif ($num>=1000 && $num<3000){
            $str = '1000-2999人';
        }elseif ($num>=3000 && $num<5000){
            $str = '3000-4999人';
        }elseif ($num>=5000 && $num<10000){
            $str = '5000-9999人';
        }else{
            $str='10000人已上';
        }
        return $str;
    }

    function random($length, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz') {
        if($chars == '0-9') {
            $chars = '0123456789';
        } else if($chars == 'a-z') {
            $chars = 'abcdefghijklmnopqrstuvwxyz';
        } else if($chars == 'A-Z') {
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        } else if($chars == 'a-Z') {
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
        }
        $str = '';
        $max = strlen($chars) - 1;
        for($i = 0; $i < $length; $i++)	{
            $str .= $chars[mt_rand(0, $max)];
        }
        return $str;
    }


    function dpassword($password, $salt) {
        return md5(($this->is_md5($password) ? md5($password) : md5(md5($password))).$salt);
    }

    function is_md5($password) {
        return preg_match("/^[a-f0-9]{32}$/", $password);
    }


    public function ceshi($parentId){
        $ini['parent_id'] = $parentId;
        $data = EscCategory::where($ini)->get();
        if(count($data)){
             $arr = [];
             $arr[]=$parentId;
             $data = $data->toArray();
             foreach ($data as $kk=>$vv){
                 $arr[]=$vv['cat_id'];
             }
             $arrStr = implode(',',$arr);
        }else{
            $arrStr= '';
        }
        return $arrStr;

//      $data =  User::where(['user_id'=>11])->first()->toArray();
//      $data['last_time'] = date('Y-m-d H:i:s',time());
//      unset($data['user_id']);
//      for ($i=1;$i<=500;$i++){
//          $data['email'] = rand(10000000,99999999)."@qq.com";
//          $data['user_name'] = "user".rand(100000,999999);
//          $data['mobile_phone'] = '189'.rand(10000000,99999999);
//          User::insert($data);
//      }

//      echo "<pre>";
//      print_r($data);
//      echo "</pre>";
//      exit();

//        $goodsData = Goods::
//            select('goods_id','cat_id','goods_name','goods_thumb','goods_img','original_img')
//            ->get()->toArray();
//        foreach ($goodsData as $kk=>$vv){
//            $saveData['img_url'] = $vv['goods_thumb'];
//            $saveData['thumb_url'] = $vv['goods_thumb'];
//            $saveData['img_original'] = $vv['goods_thumb'];
//            DB::table('goods_gallery')->where(['goods_id'=>$vv['goods_id']])->update($saveData);
//            unset($saveData);
//        }
//        echo "<pre>";
//        print_r($goodsData);
//        echo "</pre>";
//        exit();

    }

}